﻿using System;
using System.Data;

namespace APICommond
{
    public class CompareDataTable
    {
         

        /// <summary>
        /// 比较两个datatable将两个不同数据取出来
        /// </summary>
        /// <param name="ytable">原数据</param>
        /// <param name="xtable">新数据</param>
        /// <returns></returns>
        public static DataTable CompareWithDataTable(DataTable ytable, DataTable xtable)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Bdzd",typeof(string));
            table.Columns.Add("zcdm",typeof(string));
            table.Columns.Add("zcmc", typeof(string));
            table.Columns.Add("bdlx", typeof(string));
            table.Columns.Add("bdq",typeof(string));
            table.Columns.Add("bdh", typeof(string));
            foreach (DataColumn col in xtable.Columns)
            {
                foreach (DataRow row in xtable.Rows)
                {
                    string zcdm = row["zcdm"].ToString().Trim();
                    string zcmc= row["zcmc"].ToString().Trim();
                    DataRow dr = ytable.Select("zcdm='" + zcdm + "'")[0];
                    if (dr != null)
                    {
                        string xz = row[col.ColumnName].ToString().Trim();
                        string yz = dr[col.ColumnName].ToString().Trim();
                        if (xz != yz)
                        {
                            DataRow dataRow = table.NewRow();
                            dataRow["Bdzd"] = col.ColumnName;
                            dataRow["zcdm"] = zcdm;
                            dataRow["zcmc"] = zcmc;
                            dataRow["bdlx"] = "";
                            dataRow["bdq"] = yz;
                            dataRow["bdh"] = xz;
                            table.Rows.Add(dataRow);
                        }
                    }
                }
            }
            return table;
        }


        public static DataTable TableInsertRow(DataTable table, int col)
        {
           
            int index = 1;
            for (int i = col; i < table.Columns.Count; i++)
            {
                table.Columns[i].ColumnName = index + ":" + table.Columns[i].ColumnName;
                index++;
            }
            
            return table;
        }


    }
}
